import dayjs from 'dayjs'
const WEEKS_LIST = ['日', '一', '二', '三', '四', '五', '六']

// 获得日期list
export function getDayList(start: string, end: string) {
  let startDay = dayjs(start)
  const endDay = dayjs(end)
  const today = dayjs()
  const length = endDay.diff(startDay, 'day') + 1
  const res = []
  for (let index = 0; index < length; index++) {
    res.push({
      week: WEEKS_LIST[startDay.day()],
      month: startDay.month() + 1,
      date: startDay.date(),
      fullDate: startDay.format('YYYY-MM-DD'),
      today: startDay.isSame(today, 'day'),
    })
    startDay = startDay.add(1, 'day')
  }
  return res
}

// 一个小时像素
// const HOUR_PX = 2，改为动态宽度
// 计算bar的width和left
export function calcBarPosition(min: string, max: string, start: string, end: string, HOUR_PX: number) {
  // 最小
  const minTime = dayjs(min).startOf('day')
  // 最大
  const maxTime = dayjs(max).endOf('day')
  // 开始时间
  let startTime = dayjs(start)
  // 结束时间
  let endTime = dayjs(end)
  if (startTime.isBefore(minTime)) {
    startTime = minTime
  }
  if (endTime.isAfter(maxTime)) {
    endTime = maxTime
  }
  if (startTime.isAfter(maxTime)) {
    return {
      width: 0,
      left: 0,
    }
  }

  // 计算距离左侧的小时数
  let MarginLeftHour = startTime.diff(minTime, 'hour')
  if (MarginLeftHour < 0) {
    MarginLeftHour = 0
  }
  // 计算宽度小时数
  let widthHour = endTime.diff(startTime, 'hour')
  // 如果结束时间有分钟，则宽度增加一小时
  if (endTime.minute()) {
    widthHour++
  }
  // 确保宽度小时数不为负
  widthHour = Math.max(0, widthHour)

  // 转为像素
  const left = Math.ceil(MarginLeftHour * HOUR_PX)
  const width = Math.ceil(widthHour * HOUR_PX) - 1
  // 确保宽度至少为20像素，但如果是0则保持为0
  const safeWidth = width === 0 ? 0 : Math.max(20, width)

  return {
    left,
    width: safeWidth,
  }
}

// 初始化库存数据before
// 处理车辆占用列表
const dealCarUseDate = (carUseDate: any) => {
  const columns = []
  // 处理不占用数据放在第一行，被占用数据遮挡
  for (const it of carUseDate) {
    if (columns.length == 0 || it.typeSet.includes(5)) {
      columns.push([it])
    } else {
      columns[0].push(it)
    }
  }
  if (columns.length == 0) {
    columns.push([])
  }
  // 删除columns第一个column typeSet 里的 5
  if (columns[0][0]) {
    columns[0][0].typeSet = columns[0][0].typeSet.filter((it: any) => it != 5)
  }
  return columns
}

// 初始化库存数据
export function ininStockData(list: any) {
  list.forEach((group: any) => {
    group.columnNum = 0
    // 车辆占用列表
    group.columns = []
    group.car_columns = []
    group.left_columns = []
    group.carList.forEach((car: any) => {
      const columns = dealCarUseDate(car.carUseDate)
      group.columns = [...group.columns, ...columns]
      group.columnNum = group.columns.length
      const car_column = Array.from({ length: columns.length }, () => ({
        carId: car.carId,
        licenseNo: car.licenseNo,
        clash: true,
        clashTip: false,
        border: false,
      }))
      if (car_column.length) {
        car_column[0].clash = false
        car_column[car_column.length - 1].border = true
      }
      if (car_column[1]) {
        car_column[1].clashTip = true
      }
      group.car_columns.push(...car_column)
    })
  })
  return list
}
// 初始化库存数据end
